// pages/myCoupons/myCoupons.js
import * as API_Coupon from '../../api/coupon'
import * as API_MallCoupon from '../../api/mall/coupons'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cdkCode:'',
    headshow:0,
    tab_index:0,
    listArray:[],
    list:[],
    tempIndex:0,
    params:{
      pageNum:1,
      pageSize:20
    },
    finished:false,
  }, 
 
  switchTab:function(e){
  
    let current=e.currentTarget.dataset.tab_index;
    let state=e.currentTarget.dataset.state;
    if(current == this.data.tempIndex){
      return
    }
   this.switchTabToFlush(current,state);
  },
  switchTabToFlush:function(current,state){
    this.data.params = {
      pageNum:1,
      pageSize:20
    }
    switch(state){
      case 'wait_use':
        this.setData({
          tab_index:current,
          'params.canUseState':'Y',
          'params.useState':'N',
          'params.pageNum':1,
          list:[],
          tempIndex:current
          // scrollhandle:false,tempIndex:current
        });
        break;
      case 'used':
        this.setData({
          tab_index:current,
          'params.useState':'Y',
          'params.pageNum':1,
          list:[],
          tempIndex:current
          // scrollhandle:false,tempIndex:current
        });
        break;
      case 'time_out':
        this.setData({
          tab_index:current,
          'params.overdueStateParam':'Y',
          'params.pageNum':1,
          list:[],
          tempIndex:current
          // scrollhandle:false,tempIndex:current
        });
        break;
    }
    //此处setData会触发tabIndexChange，所以不用主动调用tabIndexChangeHandle，避免多次执行
    this.setData({
      tab_index:current,
      tempIndex:current
      // scrollhandle:false,tempIndex:current
    });
    this.getList()
    
  },

  tabIndexChange:function(e){
  let current=e.detail.current;
  if(current == this.data.tempIndex){
    return
  }
  this.tabIndexChangeHandle(current);
  this.setData({scrollhandle:true})
  },
  tabIndexChangeHandle:function(current){
    this.setData({tab_index:current,tempIndex:current});
    if(this.data.scrollhandle){
      //手动滑动才需要滑动到对应位置
      wx.createSelectorQuery()
      .select('.select_tab_view')
      .node()
      .exec((res) => {
        const scrollView = res[0].node;
        scrollView.scrollIntoView(".act")
      })
    }
  },
  nextSwiper:function(){
    let len=3;
    let current=this.data.tab_index;
    let next=current+1;
    if(next>len-1){
      next=0;
    }
    this.tabIndexChangeHandle(next);
  },
  showRightRecal:function(){
    //手动滑动才需要滑动到对应位置
    let that=this;
    wx.createSelectorQuery()
    .selectAll('.item')
    .boundingClientRect(function (rect) {
      let len= rect.length;
      if(len>0){
        //有元素
        let lastItem= rect[len-1];
        let right= Number(lastItem.right);
        if(right>getApp().globalData.clientWidth){
          that.setData({headshow:1});
        }
      }
    }).exec();
  },
  scroll :function(e){
    
    let deltaX=e.detail.deltaX;
    let scrollWidth=e.detail.scrollWidth;
    let scrollLeft=e.detail.scrollLeft+80;
    let clientWidth=getApp().globalData.clientWidth;
    let rightX=scrollWidth-clientWidth;
    let opacity = 1
    if(rightX<scrollLeft){
      console.info('需要隐藏')
      
      opacity = (100-(scrollLeft- rightX)*3)/100
      console.info("opacity="+opacity)
    }
    this.setData({headshow:opacity})
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let globalData=getApp().globalData;
    let windowHeight=globalData.windowHeight;
    let navHeadHeightpx=globalData.navHeadHeightpx;
    this.setData({navHeadHeightpx:navHeadHeightpx,windowHeight:windowHeight});
    this.setData({
      'params.canUseState':'Y',
      'params.useState':'N',
    })
    this.getList()
  },
  getList(){
    API_Coupon.getList(this.data.params).then(res=>{
      let arr = this.data.list
      arr.push(...res.data.rows)
      this.setData({
        list:arr,
        finished:res.data.lastPage
      })
    })
  },
  goUse(e){
    let coupon = e.currentTarget.dataset.coupon
    if(coupon.modelType == 'CM'){
      if(coupon.belongType == 'store'){
        wx.navigateTo({
          url: '/pages/storeHome/storeHome?id='+coupon.storeId,
        })
      }else if(coupon.belongType == 'platform'){
        wx.switchTab({
          url: '/pages/home/home',
        })
      }
    }
    if(coupon.modelType == 'MCM'){
      wx.switchTab({
        url: '../mall/home/home',
      })
    }
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.finished){
      return
    }
    var page = this.data.params.pageNum + 1 
    this.setData({
      'params.pageNum':page
    })
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  exchange(){
    if(!this.data.cdkCode){
      wx.showToast({
        title: '请输入兑换码',
        icon:'none',
        duration:1500
      })
      return
    }
    let that=this;
    API_MallCoupon.excechange(this.data.cdkCode).then(res=>{
        if(res.code == 200){
          this.setData({
            cdkCode:''
          })
          setTimeout(() => {
            that.switchTabToFlush(0,'wait_use');
          }, 1500);
         
        }
        wx.showToast({
          title: res.msg,
          icon:'none',
          duration:1500
        })
    })
  },
  changeCdk(e){
    this.setData({
      cdkCode:e.detail.value
    })
  },
})
